//
//  AppDelegate.m
//  豆瓣
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "UserViewController.h"
#import "ActivityListViewController.h"
#import "CinemaViewController.h"
#import "FilmListViewController.h"
#import <sqlite3.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    // 活动
    ActivityListViewController *activityVC = [[[ActivityListViewController alloc] init] autorelease];
    UINavigationController *activityNC = [[[UINavigationController alloc] initWithRootViewController:activityVC] autorelease];
    activityNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"活动" image:[UIImage imageNamed:@"activity.png"] tag:10001] autorelease];
    [activityNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    activityNC.navigationBar.tintColor = [UIColor whiteColor];
    
    
    // 电影
    FilmListViewController *filmVC = [[[FilmListViewController alloc] init] autorelease];
    UINavigationController *filmNC = [[[UINavigationController alloc] initWithRootViewController:filmVC] autorelease];
    filmNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"电影" image:[UIImage imageNamed:@"movie.png"] tag:10002] autorelease];
    filmNC.navigationBar.tintColor = [UIColor blackColor];
    [filmNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    
    // 影院
    CinemaViewController *cinemaVC = [[[CinemaViewController alloc] init] autorelease];
    UINavigationController *cinemaNC = [[[UINavigationController alloc] initWithRootViewController:cinemaVC] autorelease];
    cinemaNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"影院" image:[UIImage imageNamed:@"cinema.png"] tag:10003] autorelease];
    [cinemaNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    
    
    // 我的
    UserViewController *userVC = [[[UserViewController alloc] init] autorelease];
    UINavigationController *userNC = [[[UINavigationController alloc] initWithRootViewController:userVC] autorelease];
    userNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"user.png"] tag:10004] autorelease];
    [userNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav.png"] forBarMetrics:UIBarMetricsDefault];
    userNC.navigationBar.tintColor = [UIColor blackColor];
    

    UITabBarController *tabBC = [[UITabBarController alloc] init];
    tabBC.viewControllers = [NSArray arrayWithObjects:activityNC, filmNC, cinemaNC, userNC, nil];
    
    tabBC.tabBar.translucent = NO;
    
    self.window.rootViewController = tabBC;
    
    [tabBC release];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
//    NSLog(@"1");
//    // 打开数据库
//    static sqlite3 *DB = nil;
//    NSString *dbPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
//    NSString *filePath = [dbPath stringByAppendingPathComponent:@"dataBase.db"];
//    sqlite3_open(filePath.UTF8String, &DB);
//    
//    // 把所有状态置为 - F
//    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM user WHERE  userState LIKE '%%%@%%'", @"T"];
//    sqlite3_stmt *stmt = nil;
//    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
//    while (SQLITE_ROW == sqlite3_step(stmt)) {
//        
//       NSString *sqlStr = [NSString stringWithFormat:@"UPDATE user SET userState = '%@'", @"F"];
//       sqlite3_exec(DB, sqlStr.UTF8String, NULL, NULL, NULL);
//       return;
//    }
//    sqlite3_close(DB);
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
